import request from '@/utils/request'

export default {
  name: 'insurance-stats',
  components: {},
  data() {
    return {
      query: {
        brand: undefined,
        company: undefined,
        provinceId: undefined,
        campusLevel: undefined,
        campusLevel: undefined
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 50,
        sort: 'desc',
        sidx: 'company'
      },
      brandOptions: [],
      companyOptions: [{ enCode: '总部区域', fullName: '总部区域' }, { enCode: '河北分公司', fullName: '河北分公司' }, { enCode: '山东分公司', fullName: '山东分公司' }, { enCode: '西安分公司', fullName: '西安分公司' }],
      levelOptions: [],
      statusOptions: []
    }
  },
  computed: {},
  created() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
      this.query.brand = this.brandOptions.length > 0 ? this.brandOptions[0].enCode : undefined
      this.initData()
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'CampusLevel' }).then(res => {
      this.levelOptions = res
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'CampusStatus' }).then(res => {
      this.statusOptions = res
    })
  },
  mounted() {},
  methods: {
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/insurance/stats`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    getInsuranceRateType(rate) {
      if (rate >= 80) return 'success'
      if (rate >= 60) return 'warning'
      return 'danger'
    },
    exportData() {
      this.query.dataType = 1
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/insurance/stats/export`,
        method: 'GET',
        data: query
      }).then(res => {
        this.$message({
          type: 'success',
          message: '导出任务已提交，请稍后查看下载中心'
        })
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 50,
        sort: 'desc',
        sidx: 'company'
      }
      this.initData()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 50,
        sort: 'desc',
        sidx: 'company'
      }
      this.initData()
    }
  }
}
